import { reactive, computed, toRefs } from 'vue';
import { PaginationQuery, Pagination } from '@/types/table';
import { TableState, CheckAble } from '@/types/table';

import { getTemplateData } from '@/api/templates';

const tableQuery: PaginationQuery = reactive({
    page: 0,
    size: 10,
    sort: 'id,desc',
    template: '',
    tagColour: '',
    tagRatio: '',
});

// 编辑用户
const state: TableState & CheckAble = reactive({
    dataSource: [],
    selectedRow: {},
    selectedRowKeys: [],
    visible: false,
    loading: false,
    type: 'add',
    pagination: {
        total: 0,
        current: computed(() => tableQuery.page + 1),
    },
});

// 页码改变
const handleTableChange = (pagination: Pagination) => {
    const { current } = pagination;
    tableQuery.page = current - 1;
    getskuGoodsSkuList(tableQuery);
};

// 获取表格数据
const getskuGoodsSkuList = async (params: PaginationQuery) => {
    const { content, totalElements } = await getTemplateData(params);
    state.loading = false;
    state.dataSource = content;
    state.pagination.total = totalElements;
};

export default {
    tableQuery,
    handleTableChange,
    getskuGoodsSkuList,
    ...toRefs(state),
};
